<?php

namespace App\Models\Applet\Other;

class SettingModel extends BaseOtherModel
{
    protected $table = 'setting';

    const ALIAS_SPONSOR_SWITCH = 'sponsor_switch'; // 认养开关关键字

    const STATUS_YES = 1; // 0 已关闭 1 已打开

    /**
     * @param $alias
     * @return array
     */
    public function getAliasValue($alias): array
    {
        $result = $this->where('alias', $alias)
                       ->where('status', self::STATUS_YES)
                       ->first();

        if (empty($result['values'])) {
            return [];
        }
        return json_decode($result['values'], true);
    }

    /**
     * 总条数
     * @return void
     */
    public function total()
    {
        return $this->where('status', self::STATUS_YES)
                    ->count();
    }

    /**
     * 列表
     * @return void
     */
    public function list(array $fields = self::QUERY_DEFAULT_FIELDS)
    {
        return $this->select($fields)
                    ->where('status', self::STATUS_YES)
                    ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
                    ->limit(PAGE_SIZE)
                    ->get()
                    ->toArray();
    }

    /**
     * @return void
     */
    public function updateItemByAlias(string $alias, array $updateData)
    {
        return $this->where('alias', $alias)
                    ->update($updateData);
    }
}
